#include "client.h"

int userinfo(int sockfd){
    int recvflag;
    train_t sendTrain;
    userinfo_t usinfo;
    while(1){
        printf("*****************\n");
        printf("***1.login in****\n");
        printf("***2.sign up*****\n");
        printf("*****************\n");
        printf("Plesse choose :\n");
        fflush(stdout);
        bzero(sendTrain.buf,sizeof(sendTrain.buf));
        bzero(usinfo.username,sizeof(usinfo.username));
        bzero(usinfo.password,sizeof(usinfo.password));
        int userchose;
        scanf("%d",&userchose);
        fflush(stdin);
        if(userchose == 1){
            sendTrain.status = OD_LOG;
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);

            while(1){
                printf("Please enter username :\n");
                fflush(stdout);
                read(STDIN_FILENO,&usinfo.username,sizeof(usinfo.username));
                fflush(stdin);
                sendTrain.length = strlen(usinfo.username);
                bzero(sendTrain.buf,sizeof(sendTrain.buf));
                strcpy(sendTrain.buf,usinfo.username);
                send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
                send(sockfd,&sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);
                recvn(sockfd,&recvflag,sizeof(int));
                if(recvflag == 0){
                    break;
                }else{
                    printf("Username wrong !\n");
                    fflush(stdout);
                    continue;
                }
            }
            
            int count = 0;
            for(int i = 0 ;i < 5;i++){
                printf("Please enter password :\n");
                fflush(stdout);
                read(STDIN_FILENO,&usinfo.password,sizeof(usinfo.password));
                fflush(stdin);
                sendTrain.length = strlen(usinfo.password);
                bzero(sendTrain.buf,sizeof(sendTrain.buf));
                strcpy(sendTrain.buf,usinfo.password);
                send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
                send(sockfd,&sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);
                recvn(sockfd,&recvflag,sizeof(int));
                if(recvflag == 0){
                    printf("User login in success \n");
                    fflush(stdout);
                    return 0;
                }else{
                    if(count < 5){
                        printf("username is wrong ! Please enter password again \n");
                        fflush(stdout);
                        count+=1;
                        continue;
                    }else{
                        return 2;
                    }
                }
            }
            
        }else if(userchose == 2){
            sendTrain.status = OD_REG;
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);

            while(1){
                printf("Please enter new username : \n");
                fflush(stdout);
                read(STDIN_FILENO,usinfo.username,sizeof(usinfo.username));
                fflush(stdin);
                sendTrain.length = strlen(usinfo.username);
                bzero(sendTrain.buf,sizeof(sendTrain.buf));
                strcpy(sendTrain.buf,usinfo.username);
                send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
                send(sockfd,&sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);
                recvn(sockfd,&recvflag,sizeof(int));
                if(recvflag == 0){
                    break;
                }else{
                    printf("The user name already exists \n");
                    fflush(stdout);
                    continue;
                }
            }

            printf("Please enter new user password :\n");
            fflush(stdout);
            read(STDIN_FILENO,usinfo.password,sizeof(usinfo.password));
            fflush(stdin);
            sendTrain.length = strlen(usinfo.password);
            bzero(sendTrain.buf,sizeof(sendTrain.buf));
            strcpy(sendTrain.buf,usinfo.password);
            send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,&sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);
            recvn(sockfd,&recvflag,sizeof(int));
            if(recvflag == 0){
                printf("New user sign up success \n");
                fflush(stdout);
                continue;
            }
        }else{
            printf("Invalid input information \n");
            fflush(stdout);
            continue;
        }
    }
    return 0;
}